package com.sinosoft.tk.mybatis.mapper.provider;

import com.sinosoft.tkmybatis.mapper.util.MyExampleSqlHelp;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.MapperException;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.*;

import java.util.Iterator;
import java.util.Set;

/**
 * @Auther: zouren
 * @Date: 2019/3/29 09:02
 * @Description:
 */
public class MyBaseMapperProvider  extends MapperTemplate {

    public MyBaseMapperProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String insertNoEmpty(MappedStatement ms)
    {


            Class<?> entityClass = this.getEntityClass(ms);
            StringBuilder sql = new StringBuilder();
            Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
            this.processKey(sql, entityClass, ms, columnList);
            sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));
            sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
            Iterator var5 = columnList.iterator();

            EntityColumn column;
            while(var5.hasNext()) {
                column = (EntityColumn)var5.next();
                if (column.isInsertable()) {
                    if (column.isIdentity()) {
                        sql.append(column.getColumn() + ",");
                    } else {
                        sql.append(SqlHelper.getIfNotNull(column, column.getColumn() + ",", true));
                    }
                }
            }

            sql.append("</trim>");
            sql.append("<trim prefix=\"VALUES(\" suffix=\")\" suffixOverrides=\",\">");
            var5 = columnList.iterator();

            while(var5.hasNext()) {
                column = (EntityColumn)var5.next();
                if (column.isInsertable()) {
                    if (column.isIdentity()) {
                        sql.append(SqlHelper.getIfCacheNotNull(column, column.getColumnHolder((String)null, "_cache", ",")));
                    } else {
                        sql.append(SqlHelper.getIfNotNull(column, column.getColumnHolder((String)null, (String)null, ","), true));
                    }

                    if (column.isIdentity()) {
                        sql.append(SqlHelper.getIfCacheIsNull(column, column.getColumnHolder() + ","));
                    }
                }
            }

            sql.append("</trim>");
            return sql.toString();


    }
    private void processKey(StringBuilder sql, Class<?> entityClass, MappedStatement ms, Set<EntityColumn> columnList) {
        Boolean hasIdentityKey = false;
        Iterator var6 = columnList.iterator();

        EntityColumn column;
        label34:
        do {
            while(true) {
                while(var6.hasNext()) {
                    column = (EntityColumn)var6.next();
                    if (column.isIdentity()) {
                        sql.append(SqlHelper.getBindCache(column));
                        if (hasIdentityKey) {
                            continue label34;
                        }

                        SelectKeyHelper.newSelectKeyMappedStatement(ms, column, entityClass, this.isBEFORE(), this.getIDENTITY(column));
                        hasIdentityKey = true;
                    } else if (column.getGenIdClass() != null) {
                        sql.append("<bind name=\"").append(column.getColumn()).append("GenIdBind\" value=\"@tk.mybatis.mapper.genid.GenIdUtil@genId(");
                        sql.append("_parameter").append(", '").append(column.getProperty()).append("'");
                        sql.append(", @").append(column.getGenIdClass().getCanonicalName()).append("@class");
                        sql.append(", '").append(this.tableName(entityClass)).append("'");
                        sql.append(", '").append(column.getColumn()).append("')");
                        sql.append("\"/>");
                    }
                }

                return;
            }
        } while(column.getGenerator() != null && column.getGenerator().equals("JDBC"));

        throw new MapperException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个MySql的自动增长列,最多只能有一个!");
    }
}